package com.infitecs.eyas.quality;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.judge.service.JudgeService;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.quality.service.IndustryService;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Demon on 2015/4/23.
 */
@Controller
@RequestMapping("/quality/industry/")
public class IndustryController {
    @Autowired
    private IndustryService industryService;
    @Autowired
    private StepService stepService;

    @RequestMapping(value = "industrySave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse industrySave(Industry industry) {
        try {
            industryService.industrySave(industry);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }

    @RequestMapping(value = "calculateMf", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse calculateMf(HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        String mf = String.valueOf(industryService.calculateMfByFormula15(caseId));
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
        return new AjaxResponse(mf);
    }

    @RequestMapping(value = "calculateVdaf", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse calculateVdaf(HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        String vdaf = String.valueOf(industryService.calculateVdafByFormula20(caseId));
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
        return new AjaxResponse(vdaf);
    }

    @RequestMapping(value = "calculateVar", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse calculateVar(HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        String var = String.valueOf(industryService.calculateVarByFormula19(caseId));
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
        return new AjaxResponse(var);
    }

    @RequestMapping(value="save",method = {RequestMethod.GET,RequestMethod.POST})
    public String save(HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        stepService.updateStepStateFinish(caseId, Constant.STEP_ID_1);
        return "redirect:/quality/element?menuId=9";
    }
    @RequestMapping(value = "updateMar",method = RequestMethod.POST)
    @ResponseBody
    public void updateMar(@RequestParam("mar")double mar,HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        industryService.updateMar(mar,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
    }

    @RequestMapping(value = "updateMad",method = RequestMethod.POST)
    @ResponseBody
    public void updateMad(@RequestParam("mad") double mad,HttpSession httpSession){
        int caseId =  Integer.parseInt(httpSession.getAttribute("caseId").toString());
        industryService.updateMad(mad,caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
    }

    @RequestMapping(value = "updateAar",method = RequestMethod.POST)
    @ResponseBody
    public void updateAar(@RequestParam("aar") double aar,HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        industryService.updateAar(aar, caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
    }

    @RequestMapping(value = "updateVdaf",method = RequestMethod.POST)
    @ResponseBody
    public void updateVdaf(@RequestParam("vdaf") double vdaf,HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        industryService.updateVdaf(vdaf, caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
    }

    @RequestMapping(value = "updateVar",method = RequestMethod.POST)
    @ResponseBody
    public void updateVar(@RequestParam("var") double var,HttpSession httpSession){
        int caseId = Integer.parseInt(httpSession.getAttribute("caseId").toString());
        industryService.updateVar(var, caseId);
        stepService.updateStepStateing(caseId, Constant.STEP_ID_1);
    }

    @RequestMapping(value = "recover", method = RequestMethod.GET)
    @ResponseBody
    public Industry getIndustry(HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        return industryService.getIndustry(caseId);
    }
}
